一個遊戲少不了的就是按鈕的設定及美編啦~今天增加的設定可以初步調整簡單且無造型的遊戲,提高遊戲客觀性!
簡介
/設計一個簡單的進入遊戲前的頁面視窗(讓玩家可以選擇開始遊戲、查看說明或退出遊戲)
/使用Java Swing來創建這個圖形化的界面。
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GameLauncher extends JFrame {
private int width = 21;
private int height = 21;
private ChallengingMaze maze;
public GameLauncher() {
setTitle("挑戰迷宮");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 設置版面
setLayout(new BorderLayout());
// 標題
JLabel titleLabel = new JLabel("挑戰迷宮", SwingConstants.CENTER);
titleLabel.setFont(new Font("Serif", Font.BOLD, 24));
add(titleLabel, BorderLayout.NORTH);
// 按鈕面板
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 1, 10, 10));
// 開始遊戲按鈕
JButton startButton = new JButton("開始遊戲");
startButton.setFont(new Font("Serif", Font.PLAIN, 18));
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startGame();
}
});
buttonPanel.add(startButton);
// 說明按鈕
JButton helpButton = new JButton("遊戲說明");
helpButton.setFont(new Font("Serif", Font.PLAIN, 18));
helpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showHelp();
}
});
buttonPanel.add(helpButton);
// 退出按鈕
JButton exitButton = new JButton("退出遊戲");
exitButton.setFont(new Font("Serif", Font.PLAIN, 18));
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
buttonPanel.add(exitButton);
add(buttonPanel, BorderLayout.CENTER);
}
private void startGame() {
maze = new ChallengingMaze(width, height);
for (int level = 1; level <= 5; level++) {
maze.generateMaze(level);
System.out.println("關卡 " + level);
maze.printMaze();
maze.moveCharacter(10, 10);
System.out.println("關卡 " + level + " 的得分: " + maze.getScore());
System.out.println();
}
JOptionPane.showMessageDialog(this, "遊戲結束!\n最終得分: " + maze.getScore(), "遊戲結束", JOptionPane.INFORMATION_MESSAGE);
}
private void showHelp() {
JOptionPane.showMessageDialog(this, "在迷宮中探索並找到寶藏。每個關卡敵人和陷阱會逐漸增加。\n控制角色移動並躲避陷阱,找到寶藏獲得更高分數。", "遊戲說明", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GameLauncher().setVisible(true);
}
});
}
}
class ChallengingMaze {
private final int width;
private final int height;
private char[][] maze;
private final Random random = new Random();
private int score;
public ChallengingMaze(int width, int height) {
this.width = width;
this.height = height;
this.score = 0;
}
public void generateMaze(int level) {
maze = new char[width][height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
maze[x][y] = '#'; // 用#表示牆壁
}
}
// 設置空白路徑
for (int x = 1; x < width - 1; x++) {
for (int y = 1; y < height - 1; y++) {
maze[x][y] = ' ';
}
}
// 添加敵人和陷阱
int enemies = level;
int traps = level;
int treasures = level;
for (int i = width - 2; i >= 1; i--) {
for (int j = height - 2; j >= 1; j--) {
if (random.nextInt(100) < 5 && enemies > 0) {
maze[i][j] = 'E'; // E表示敵人
enemies--;
} else if (random.nextInt(100) < 5 && traps > 0) {
maze[i][j] = 'T'; // T表示陷阱
traps--;
} else if (random.nextInt(100) < 5 && treasures > 0) {
maze[i][j] = 'C'; // C表示寶藏
treasures--;
}
}
}
}
public void printMaze() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
System.out.print(maze[x][y] + " ");
}
System.out.println();
}
}
public void moveCharacter(int startX, int startY) {
int x = startX;
int y = startY;
while (true) {
if (maze[x][y] == 'T') {
score -= 10;
System.out.println("踩到陷阱,扣10分");
} else if (maze[x][y] == 'C') {
score += 10;
System.out.println("找到寶藏,加10分");
} else if (maze[x][y] == 'E') {
score -= 20;
System.out.println("遇到敵人,扣20分");
}
maze[x][y] = ' '; // 清除標記
// 隨機移動到下一個位置
int[] directions = {0, 1, 0, -1, 1, 0, -1, 0}; // 上、右、下、左
int dirIndex = random.nextInt(4) * 2;
x += directions[dirIndex];
y += directions[dirIndex + 1];
if (x < 0 || x >= width || y < 0 || y >= height || maze[x][y] == '#') {
break; // 碰到牆壁或邊界時停止移動
}
}
}
public int getScore() {
return score;
}
}
往後幾天的目標
1.將迴圈遊戲整合並延伸關卡的難度
2.延伸Java swing觀念